<?php
namespace App\Repository\Db;

use App\Exceptions\ValidateException;
use App\Repository\Contracts\GoodsInterface;
use App\Repository\Contracts\ShoppingCartInterface;
use App\Repository\Contracts\TaoCangInterface;
use Illuminate\Support\Facades\DB;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 15:02
 *
 * @Description:
 */
class ShoppingCartRepository extends Repository implements ShoppingCartInterface
{
    protected $rules = [
        'created' => [
            'uid' => 'sometimes|exists:member,uid',
            'goods_id' => 'required',
            'admin_id' => 'required|exists:admin,uid',
        ],
        'updated' => [
            'counts' => 'required|integer'
        ]
    ];

    protected $messages = [
        'uid.exists' => "会员不存在",
        'goods_id.required' => "商品 ID 不能为空",
        'admin_id.required' => "管理员 ID 不能为空",
        'admin_id.exists' => "管理员不存在",
    ];

    protected function modelName()
    {
        return 'App\Model\ShoppingCart';
    }

    public function lists($map = [])
    {
        return $this->searchMap($map)->orderBy('id', 'desc')->get();
    }

    public function created(array $data)
    {
        $this->validate($data, 'created');
        $query = $this->model->where('goods_id', '=', $data['goods_id'])
            ->where('admin_id', '=', $data['admin_id'])->where('status', '=', 1);
        if ($data['uid']) {
            $query = $query->where('uid', '=', $data['uid']);
        }
        $cart = $query->first();
        if ($cart) {
            return $this->increment($cart->id, 'counts', 1);
        }

        $goods = $this->container->make(GoodsInterface::class)->detail($data['goods_id']);
        if ($goods) { //&& $goods->status === 1
            $data['article_no'] = $goods->article_no?$goods->article_no:$goods->bar_code;
            $data['zh_name'] = $goods->zh_name;
            $data['en_name'] = $goods->en_name;
            $data['price'] = $goods->price;
            $data['counts'] = 1;
            return $this->save($data);
        }

        throw new ValidateException('商品已下架或不存在');

    }

    public function updateCount($id, $counts)
    {
        // 验证
        $this->validate(['counts' => $counts], 'updated');

        return $this->model->where('status', '=', 1)->where('goods_id', '=', $id)->update(['counts' => $counts]);
    }

    public function pending($uid)
    {
        return $this->model->where('admin_id', '=', $uid)->where('status', '=', 2)->groupBy(['pending_no'])->get();
    }

    public function createdPending($uid)
    {
        if ($this->model->where('admin_id', '=', $uid)->where('status', '=', 1)->first())
            return $this->model->where('admin_id', '=', $uid)->where('status', '=', 1)
                ->update(['pending_no' => date('YmdHis', time()) . mt_rand(100, 1000), 'status' => 2]);

        throw new ValidateException('操作失败');
    }

    public function cancelPending($uid, $pendingNo)
    {
        if ($this->model->where('admin_id', '=', $uid)->where('pending_no', '=', $pendingNo)->first())
            return $this->model->where('admin_id', '=', $uid)->where('pending_no', '=', $pendingNo)->delete();

        throw new ValidateException('操作失败');
    }

    public function getPending($uid, $pendingNo)
    {
        if ($this->model->where('admin_id', '=', $uid)->where('pending_no', '=', $pendingNo)->first())
            return $this->update(['pending_no' => 0, 'status' => 1], $pendingNo, 'pending_no');

        throw new ValidateException('操作失败');
    }

    public function deleted($ids)
    {
        return $this->model->where('status', '=', 1)->whereIn('goods_id', $ids)->delete();
    }

    public function clear($uid)
    {
        return DB::select('DELETE FROM `c_shopping_cart` WHERE `admin_id` = :admin_id AND `status` = 1',
            ['admin_id' => $uid]
        );
    }
}